package com.lujianfei.powersearch.ui.createOrUpdate

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseActivity
import com.lujianfei.powersearch.databinding.ActivityAppSelectBinding
import com.lujianfei.powersearch.ui.createOrUpdate.viewmodel.AppSelectViewModel

class AppSelectActivity: BaseActivity<ActivityAppSelectBinding>() {

    companion object {
        fun startAppSelect(context: Activity, requestCode:Int) {
            context.startActivityForResult(Intent(context, AppSelectActivity::class.java), requestCode)
        }
    }
    private val mAppSelectViewModel:AppSelectViewModel by lazy { buildViewModel() }

    override fun resourceId() = R.layout.activity_app_select

    override fun initView() {
        setTitleMiddleText("关联 App")
        binding?.vm = mAppSelectViewModel
    }

    override fun initData() {
        mAppSelectViewModel.load()
    }

    override fun initEvent() {
        mAppSelectViewModel.onItemClickCallback = {
            setResult(RESULT_OK, Intent().putExtra("packageName",it.packageName).putExtra("name", it.name))
            finish()
        }

        binding?.searchBar?.onSearchBarListener = { searchText ->
            mAppSelectViewModel.search(searchText)
        }
    }

    override val titleBarResId: Int = R.id.toolbar
}